//
//  ThemeViewController.m
//  MusicPlayer
//
//  Created by dlios on 15-6-1.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ThemeViewController.h"
#import "ThemeCollectionViewCell.h"
@interface ThemeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, retain)UICollectionView * collectView;
@property (nonatomic, retain) NSArray * array;
@end

@implementation ThemeViewController

- (void)dealloc
{
    [_array release];
    [_collectView release];
    [super dealloc];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        self.array = @[@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"7.jpg",@"8.jpg",@"9.jpg",@"10.jpg",@"11.jpg",@"12.jpg",@"13.jpg",@"14.jpg",@"15.jpg",@"16.jpg",@"17.jpg",@"18.jpg",@"19.jpg",@"20.jpg",@"21.jpg",@"22.jpg",@"23.jpg",@"24.jpg",@"25.jpg",@"26.jpg",@"27.jpg",@"28.jpg",@"29.jpg",@"31.jpg",@"80.jpg",@"81.jpg",@"82.jpg",@"83.jpg",@"PIC2.png"];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
//    UIButton *my = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width / 375 * 30, self.view.frame.size.width / 375 * 49)];
//    [my setImage:[UIImage imageNamed:@"iconfont-wode.png"] forState:UIControlStateNormal];
//    [my addTarget:self action:@selector(searchAction:) forControlEvents:UIControlEventTouchUpInside];
//    
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:my];
    
    UIBarButtonItem *myInfo = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(myInfoAction:)];
    self.navigationItem.leftBarButtonItem = myInfo;
    
    [self creatCollectionView];
    
}

//-(void)searchAction:(id)sender
//{
//    [self.navigationController popToRootViewControllerAnimated:YES];
//}

//创建collectionView
- (void)creatCollectionView
{
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    
    layout.sectionInset = UIEdgeInsetsMake(self.view.frame.size.width / 375.0* 20, self.view.frame.size.height/677.0 *20, self.view.frame.size.width/ 375.0 * 20, self.view.frame.size.height / 677.0* 20);
    //    上下之间的间隔
        layout.minimumLineSpacing =self.view.frame.size.height / 667.0 * 50;
    //    左右之间的间隔
        layout.minimumInteritemSpacing =self.view.frame.size.width / 375.0* 3;
    layout.itemSize = CGSizeMake(self.view.frame.size.width/375.0* 150,self.view.frame.size.height/677.0* 150);

    self.collectView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - self.view.frame.size.height/677.0* 70 ) collectionViewLayout:layout];
      [self.collectView registerClass:[ThemeCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    self.collectView.delegate = self;
    self.collectView.dataSource = self;
    [self.view addSubview:self.collectView];
    [self.collectView release];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.array.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ThemeCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    [cell.imageView setImage:[UIImage imageNamed:[self.array objectAtIndex:indexPath.row]]];

    return cell;
    
}

//collectionView点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [[NSUserDefaults standardUserDefaults] setObject:[self.array objectAtIndex:indexPath.row] forKey:@"pic1"];
    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"已更换成功" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
    [alert show];
    [alert release];
}

- (void)myInfoAction:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
